package com.lsabcd.dubbo.test.web.interceptor;

import com.lsabcd.dubbo.test.IpHelper;
import com.lsabcd.dubbo.test.web.util.IpUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class IpInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        IpHelper.setClientIp(IpUtil.getIpAddress(request));
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        IpHelper.clear();
        super.postHandle(request, response, handler, modelAndView);
    }
}
